package com.stylefeng.guns.core.util;


import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * <p>@Author: Willy </p>
 * <p>@Description: 时间 </p>
 * <p>@Date: 2019/1/10 16:45 </p>
 */
public class DateTimeUtil {

    // 格式：2007年06月07日 12时12分12秒234毫秒
    private final static String[] FORMAT_CHINA = {"年", "月", "日", "时", "分", "秒", "毫秒"};

    // 格式：2007-06-07 12:12:12 234
    private final static String[] FORMAT_NORMAL = {"-", "-", "", ":", ":", "", ""};

    // 格式：2007/06/07 12:12:12 234
    private final static String[] FORMAT_DATATIME = {"/", "/", "", ":", ":", "", ""};


    public static final String START_TIME = " 00:00:00";
    public static final String END_TIME = "23:59:59";

    /**
     * 返回年月日
     *
     * @return yyyyMMdd
     */
    public static String getTodayChar8() {
        return DateFormatUtils.format(new Date(), "yyyyMMdd");
    }

    public static String getSystemTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(date);
    }

    public static String getCurrentDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(date);
    }

    public static String getCurrentBeginDay() {
        return getCurrentDay() + START_TIME;
    }

    public static String getCurrentEndDay() {
        return getCurrentDay() + " " + END_TIME;
    }

    public static String getSystemTime(String timezone) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        format.setTimeZone(TimeZone.getTimeZone(timezone));
        return format.format(date);
    }

    public static String getSystemTime(String timezone, String format1) {

        SimpleDateFormat format = new SimpleDateFormat(format1);
        Date date = new Date();
        format.setTimeZone(TimeZone.getTimeZone(timezone));
        return format.format(date);
    }

    public static String getDay(int day) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, day);
        Date time = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(time);
    }

    public static String getDayBegin(int day) {

        return getDay(day) + START_TIME;
    }

    public static String getDayEnd(int day) {

        return getDay(day) + " " + END_TIME;
    }

    public static Date getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            return format.parse(getSystemTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getSystemDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(date);
    }


    public static Date getTimeFormat(String timeZone, String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        try {
            Date date = dateFormat.parse(time);

            SimpleDateFormat chinaformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chinaformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            String china = chinaformat.format(date);
            SimpleDateFormat chinaformatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = chinaformatDate.parse(china);

            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return null;
    }

    public static String getTimeFormat(Date date) {
        SimpleDateFormat chinaformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        chinaformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String china = chinaformat.format(date);
        return china;

    }

    public static Date getTimeFormat(String timeZone, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        try {
//            Date date = dateFormat.parse(time);

            SimpleDateFormat chinaformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chinaformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            String china = chinaformat.format(date);
            SimpleDateFormat chinaformatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = chinaformatDate.parse(china);

            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return null;
    }

    public static Date getTimeFormat(String timeZone, String time, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        try {
            Date date = dateFormat.parse(time);

            SimpleDateFormat chinaformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chinaformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            String china = chinaformat.format(date);
            SimpleDateFormat chinaformatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = chinaformatDate.parse(china);

            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return null;
    }


    public static String getSystemTimeNotFormat() {
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(new Date());
        return DateFormatUtils.format(theCa.getTime(), "yyyyMMddHHmmss");
    }


    public static String getSystemTime4Day() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd");
    }

    public static String getSystemTime4BeginDay() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 00:00:00";
    }

    public static String getSystemTime4BeginDay(String timezone) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.setTimeZone(TimeZone.getTimeZone(timezone));
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 00:00:00";
    }


    public static String getSystemTime4EndDay(String timezone) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.setTimeZone(TimeZone.getTimeZone(timezone));
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 23:59:59";
    }


    public static String getSystemTime4Day(String timezone) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.setTimeZone(TimeZone.getTimeZone(timezone));
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd");
    }

    public static String getSystemTime4Day(String timezone, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.setTimeZone(TimeZone.getTimeZone(timezone));
        return DateFormatUtils.format(c.getTime(), format);
    }

    public static String getSystemTime41() {
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        c.add(Calendar.DATE, 1);
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd");
    }

    public static String getSystemTime4Hour() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return DateFormatUtils.format(c.getTime(), "HH:mm");

    }

    /**
     * 取得当月天数
     */
    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static String getDateDiffurplusTime(String nowDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        Date startTime = null;
        Date currentTime = new Date();
        try {
            date = simpleDateFormat.parse(nowDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 2);
            startTime = c.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // 获得两个时间的毫秒时间差异
        long diff = startTime.getTime() - currentTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        if (hour == 0) {
            return min + "分钟";
        } else if (day == 0) {
            return hour + "小时";
        } else if (day >= 1 && day <= getCurrentMonthLastDay()) {
            if (hour > -48) {
                return hour + "小时";
            } else {
                return day + "天";
            }
        } else {
            return nowDate;
        }


    }

    /**
     * 获得两个时间的秒数差
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static long getDateDiffSeconds(String beginTime, String endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate;
        Date startDate;
        try {
            endDate = simpleDateFormat.parse(endTime);
            startDate = simpleDateFormat.parse(beginTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();

        return diff / 1000;

    }

    public static long getDiff(String beginTime, String endTime, String format) {
        SimpleDateFormat dfs = new SimpleDateFormat(format);
        long between = 0;
        try {
            Date begin = dfs.parse(beginTime);
            Date end = dfs.parse(endTime);
            between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)));
        long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);


        return min;
    }


    public static boolean compareDate(String date) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date1 = null;
        Date date2 = null;
        long year = 1000 * 24 * 60 * 60;
        try {
            date1 = df.parse(date);
            date2 = df.parse(getSystemTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date2.getTime() > date1.getTime();

    }

    public static boolean isOverTime(String time) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date parse = df.parse(time);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DAY_OF_MONTH, 2);
            Date time1 = c.getTime();

            Date parse1 = df.parse(getSystemTime());
            if (parse1.getTime() >= time1.getTime()) {
                return true;
            } else {
                return false;
            }


        } catch (ParseException e) {
            return false;
        }


    }

    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }


    public static boolean isToday(String d) {

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = fmt.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (fmt.format(date).toString().equals(fmt.format(new Date()).toString())) {//格式化为相同格式
            return true;
        } else {
            return false;
        }


    }


    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime 时间区间,半闭合,如[10:00-20:00)
     * @param curTime    需要判断的时间 如10:00
     * @return
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
    }


    public static boolean compareDate(String sourceTime, long time, String unit) {
        String systemTime = getSystemTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;


        try {
            long current = df.parse(systemTime).getTime();
            long parse = df.parse(sourceTime).getTime();

            // 获得两个时间的毫秒时间差异
            long diff = current - parse;
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
            if (unit.equals("D")) {
                if (day >= time) {
                    return true;
                }
            } else if (unit.equals("H")) {
                if (hour >= time) {
                    return true;
                }
            } else if (unit.equals("M")) {
                if (min > time) {
                    return true;
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }


        return false;
    }

    /**
     * 获取过去或者未来 任意天内的日期数组
     *
     * @param intervals intervals天内
     * @return 日期数组
     */
    public static ArrayList<String> test(int intervals) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i < intervals; i++) {
            pastDaysList.add(getPastDate(i));
            fetureDaysList.add(getFetureDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     *
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取指定日志未来第 past 天
     * yyyy-MM-dd HH:mm:ss
     * @param date
     * @param past
     * @return
     */
    public static String getFetureDateTime(String date, int past){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date time = format.parse(date);
            calendar.setTime(time);
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
            Date today = calendar.getTime();
            String result = format.format(today);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定日期未来第 past 天
     * @param date
     * @param past
     * @return
     */
    public static String getFetureDate(String date, int past){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time = format.parse(date);
            calendar.setTime(time);
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
            Date today = calendar.getTime();
            String result = format.format(today);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间加法运算
     *
     * @param currentTime
     * @param opt
     * @return
     */
    public static String getNextTime(String currentTime, int opt, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = null;
        try {
            dt = sdf.parse(currentTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MINUTE, opt);
        Date dt1 = rightNow.getTime();
        String endTime = sdf.format(dt1);
//        if (!DateTimeUtil.isSameDay(currentTime, endTime, format)) {
//            endTime = currentTime.split(" ")[0] + " " + START_TIME;
//
//        }
        return endTime;

    }


    /**
     * 时间加法运算
     *
     * @param currentTime
     * @param opt
     * @return
     */
    public static String getNextTimeBySecond(String currentTime, Integer opt, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = null;
        try {
            dt = sdf.parse(currentTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.SECOND, opt);
        Date dt1 = rightNow.getTime();
        String endTime = sdf.format(dt1);
//        if (!DateTimeUtil.isSameDay(currentTime, endTime)) {
//            endTime = endTime.split(" ")[0] + " " + START_TIME;
//
//        }
        return endTime;

    }

    /**
     * 时间加法运算
     *
     * @param currentTime
     * @param opt
     * @return
     */
    public static Long getNextTime(Long currentTime, int opt, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = null;
        String format1 = sdf.format(new Date(currentTime));
        String nextTime = getNextTime(format1, opt, format);
        Date parse = null;
        try {
            parse = sdf.parse(nextTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse.getTime();

    }


    public static String dayOpt(String day, int opt, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = null;
        try {
            dt = sdf.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_MONTH, opt);
        Date dt1 = rightNow.getTime();
        String endTime = sdf.format(dt1);
        return endTime;
    }


    public static boolean isSameDay(String startTIme, String endTime, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date begin;
        Date end;

        try {
            begin = df.parse(startTIme);
            end = df.parse(endTime);

            return DateUtils.isSameDay(begin, end);

        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }


    }

    /**
     * yyyy/MM/dd HH:mm:ss 转 yyyy-MM-dd HH:mm:ss
     *
     * @param data
     * @return
     */
    public static Date getDateTime(String data) {
        SimpleDateFormat in = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        out.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        try {
            return out.parse(out.format(in.parse(data)));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符串时间转Data类型
     * @param data
     * @return
     */
    public static Date stringToData(String data) {
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        out.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        try {
            return out.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static long getDateTimeMillis(String time, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            Date parse = simpleDateFormat.parse(time);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

    public static String getDateTimeMillis() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return format.format(new Date());
    }

    /**
     * 获取当前时间前多少分钟的时间
     *
     * @param minute 分钟
     * @return
     */
    public static Long getCurrentTime(int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        try {
            beforeTime.setTime(sdf.parse(getSystemTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        beforeTime.add(Calendar.MINUTE, -minute);//
        Date beforeD = beforeTime.getTime();
//        String time = sdf.format(beforeD);
        return beforeD.getTime();
    }

    /**
     * 获取当前时间前多少分钟的时间
     *
     * @param minute 分钟
     * @return
     */
    public static Long getAddTime(int minute, Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(new Date(time));
        beforeTime.add(Calendar.MINUTE, minute);
        Date beforeD = beforeTime.getTime();
        return beforeD.getTime();
    }


    public static String getAddTimHour(int hour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(getCurrentTime());
        beforeTime.add(Calendar.HOUR, hour);
        Date beforeD = beforeTime.getTime();
        return sdf.format(beforeD);
    }


    public static String getSubTimHour(int hour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(getCurrentTime());
        beforeTime.set(Calendar.HOUR_OF_DAY, beforeTime.get(Calendar.HOUR_OF_DAY) - hour);
        Date beforeD = beforeTime.getTime();
        return sdf.format(beforeD);
    }

    /**
     * 将“2019-06-27”转换成“6.27”
     * @param date
     * @return
     */
    public static String dateFormat(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = sdf.parse(date);
            int month =date1.getMonth()+1;
            int day = date1.getDate();
            date = month+"."+day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * yyyy-MM-dd HH:mm:ss 转换为 MM.dd
     * @param date
     * @return
     */
    public static String formatDate(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = sdf.parse(date);
            int month =date1.getMonth()+1;
            int day = date1.getDate();
            date = month+"."+day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 功能描述:  与当前时间相差多少分钟
     *
     * @param nowDate
     * @param timeZone
     * @return
     * @throws ParseException
     */
    public static Long getDatePoor(Date nowDate, String timeZone) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        Date date = null;
        try {
            date = dateFormat.parse(dateFormat.format(nowDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        Long currentTime = getCurrentTime(0);
        if (timeZone.equals("GMT+00:00")) {
            currentTime = format.parse(getHbEndTime()).getTime();
        }
        Date endDate = new Date(currentTime);
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - date.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        long m = (diff / 1000) / 60;
        long h = ((diff / 1000) / 60) / 60;
        // 计算差多少秒//输出结果
//         long sec = diff % nd % nh % nm / ns;
        return m;
//        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static String getHbBetTime(String format1, int opt) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Long nextTime = getNextTime(format.parse(format1).getTime(), opt, "yyyy-MM-dd HH:mm:ss");
        Date date = new Date(nextTime);
//        format.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        return format.format(date);
    }

    public static String getHbEndTime() throws ParseException {
        Long currentTime = getCurrentTime(0);
        Date date = new Date(currentTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format2 = dateFormat.format(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        return format.format(dateFormat.parse(format2));
    }

    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 功能描述: 获取上周一日期
     *
     * @return
     */
    public static String getLastMonday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getCurrentTime());
        calendar.set(Calendar.DAY_OF_WEEK, -5);
        calendar.add(Calendar.WEEK_OF_YEAR, -1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(calendar.getTime());
        return format;
    }

    /**
     * 功能描述: 获取上周日日期
     *
     * @return
     */
    public static String getLastSunday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getCurrentTime());
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(calendar.getTime());
        return format;

    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 每周的第一天和最后一天
     *
     * @return
     * @throws ParseException
     */
    public static String getFirstOrLastOfWeek(String resultDateFormat, boolean isFirst) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateTimeUtil.getCurrentTime());
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        if (isFirst) {
            // 所在周开始日期
            return new SimpleDateFormat(resultDateFormat).format(cal.getTime());
        } else {
            cal.add(Calendar.DAY_OF_WEEK, 6);
            // 所在周结束日期
            return new SimpleDateFormat(resultDateFormat).format(cal.getTime());
        }

    }

    /**
     * 判断是否是过去的日期
     *
     * @param
     * @return
     */
    public static boolean isPastDate(String str, String now) {

        boolean flag = false;
        Date pastDate = null;
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //在日期字符串非空时执行
        if (str != null && !"".equals(str)) {
            try {
                //将字符串转为日期格式，如果此处字符串为非合法日期就会抛出异常。
                pastDate = sdf.parse(str);
                //调用Date里面的before方法来做判断
                flag = pastDate.before(simpleDateFormat.parse(now));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("日期参数不可为空");
        }
        return flag;
    }

    /**
     * 把每天时间分成每小时的区间
     */
    public static List<String> getListDateTime(String date) {
        String dBegin = date + START_TIME;
        String dEnd;
        if(date.equals(getCurrentDay())){
            dEnd = getSystemTime();
        }else {
            dEnd = dayOpt(dBegin,1,"yyyy-MM-dd HH:mm:ss");
        }
        //装返回的日期集合容器
        List<String> Datelist = new ArrayList<String>();
        Datelist.add(dBegin);

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //设置开始时间
            Calendar calBegin = Calendar.getInstance();
            calBegin.setTime(format.parse(dBegin));

            //设置结束时间
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(format.parse(dEnd));

            // 每次循环给calBegin日期加一天，直到calBegin.getTime()时间等于dEnd
            while (format.parse(dEnd).after(calBegin.getTime())) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.HOUR_OF_DAY, 1);
                Datelist.add(format.format(calBegin.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return Datelist;
    }

}
